package com.zb.utils;

import com.zb.entity.AccounLoginInfo;
import com.zb.exception.VendorIdentifierException;

import javax.servlet.http.HttpServletRequest;

/**
 * 移动端Token工具类
 * @Author WEN
 * @Date 2023/11/8 0008
 **/
public class TokenMobileUtil {

    /**
     * 从请求头中获取商家ID
     * @param request
     * @return
     */
    public static Long getCustAccId(HttpServletRequest request) throws VendorIdentifierException {
        AccounLoginInfo loginInfo = getAccountLoginInfo(request);
        if (null == loginInfo) {
            return TokenUserUtil.getCustAccIdByMobildUserCustCode(request);
        }
        return loginInfo.getCustAccId();
    }

    /**
     * 获取商家附件编码
     * @param request
     * @return
     */
    public static String getAnnexCode(HttpServletRequest request) {
        AccounLoginInfo loginInfo = getAccountLoginInfo(request);
        return null == loginInfo? null : loginInfo.getCustAccAnnexCode();
    }

    /**
     * 获取商家视频编码
     * @param request
     * @return
     */
    public static String getVideoCode(HttpServletRequest request) {
        AccounLoginInfo loginInfo = getAccountLoginInfo(request);
        return null == loginInfo? null : loginInfo.getCustAccVideoCode();
    }

    /**
     * 获取登录信息
     * @param request
     * @return
     */
    public static AccounLoginInfo getAccountLoginInfo(HttpServletRequest request) {
        // 默认先从客户报单端获取登录信息
        AccounLoginInfo loginInfo = TokenUserUtil.getAccountLoginInfo(request);
        // 如果为空，则从服务商端获取登录信息
        if (null == loginInfo) {
            loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        }
        return loginInfo;
    }

    /**
     * 是否移动端上传文件
     * @return
     */
    public static boolean isMobileUploadFile(){
        HttpServletRequest request = RequestUtil.getRequestByContext();
        String uri = request.getRequestURI();
        if (!uri.contains("/mobile/file") && !uri.contains("/file/delete/mobile")) {
            return false;
        }
        return null != getAccountLoginInfo(request);
    }



}
